<?php
/**
 * Create a new Customer
 *
 * @author Xavier VILAIN
 * @package Contact
 *
 */

use Fox\Html\Input;
use Nawa\MedicalStudy\Role;
use Fox\Message;
use Fox\Dao\SqlFragment;
use Fox\Msg;
use Fox\Generator;
use Fox\Html\Header\Image;
use Nawa\Messages;
use Fox\Html\TabPanel\Tab;
use Fox\Html\Wrapper;
use Fox\Html\TabPanel;
use Nawa\Customer;
use Fox\Html\Screen;

$screen = new Screen("eCustomer","create");
$screen->mainTitle(new Image("contact","<div class=\"page-title\">".Msg(Messages::BUNDLE,Messages::TITLE_CREATE_INVESTIGATOR)."</div>"));
$tabPanel = new TabPanel("pCustomer");
$wMasterData = new Wrapper("wMasterData");

$blName = Xview::instance()->component("customers", "name");
$blName->add(new Fox\Html\DropList(array("role","function"),true,false,true))
	->maximize()
	->options(Role::all(),'role','label');
$wMasterData->add($blName);
$blName->add(new Input(array('employee_service','service'),false));
$blAddress = Xview::instance()->component("customers", "address");
$blAddress->item("email1")->mandatory();
$wMasterData->add($blAddress);
$blConnection = Xview::instance()->component("contacts", "user");
$wMasterData->add($blConnection);
$tabPanel->add(new Tab("tMasterData",Msg(Msg::BUNDLE,Msg::TITLE_MASTER_DATA),$wMasterData));
$screen->add($tabPanel);

if($screen->submitted())
{
	$inputs = $screen->get();
	// Check Duplicates ?
	if(!$screen->errors())
	{
		$inputs['name1'] = $inputs['first_name'].' '.$inputs['last_name'];
		// Load Customizing Point
		if($inputs['applicationaccess'])
		{
			// Check Mandatory Data
			$input['email1'] = trim($inputs['email1']);
			// Check Duplicates on email
			$finder = new User();
			$res = $finder->find(new SqlFragment("email = ?",array($inputs['email1'])));
			if(count($res) > 0)
			{
				Message::Error(Msg(Msg::BUNDLE,Msg::ERR_DUPLICATE_USER,$inputs['email1'],$res[0]->fname));
			}
		}
		$finder = new Customer();
		$res = $finder->find(new SqlFragment("email1 = ?",array($inputs['email1'])),
		null,array(Customer::$JOIN_ADDRESS));
		if(count($res) > 0)
		{
			Message::Error(Msg(Msg::BUNDLE,Msg::ERR_DUPLICATE_USER,$inputs['email1'],$res[0]->name1));
		}
	}


	if(!$screen->errors())
	{
		try {
			// Default Values
			$inputs['type'] = Customer::TYPE_EMPLOYEE;
			// Create Customer
			$object = new Customer();
			$object->setFields($inputs);
			$object->create();
			$id = $object->getId();
			Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_CREATE,$object->name1));
			// Email Notification
			if(!empty($inputs['notify']) && !empty($inputs['applicationaccess']))
			{
				$object->user()->notifyCreation(\Xview::instance()->application()->title,_URL_);
				Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_NOTIFICATION_MAIL_SENT,$inputs['email1']));
			}
			$screen->redirect(Generator::url('contacts','update',null,array('customerid' => $id)));
		}
		catch(Fox\Object\Exception\Save $e)
		{
			Fox\Message::Error($e->getMessage());
			//var_dump($e);
		}
	}
}
echo $screen->Display();